package com.loper7.base.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.loper7.base.utils.BackHelper;
import com.loper7.base.utils.UIHelper;
import com.loper7.base.widget.dialog.SweetAlertDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 应用程序Fragment的基类
 *
 * @author
 * @version 1.0
 * @created 2012-9-18
 */
public abstract class BaseFragment extends Fragment {
    protected Context context;
    protected Unbinder unbinder;
    protected BackHelper backHelper;
    protected View view = null;// infalte的布局
    public SweetAlertDialog loadingDialog;

    public BaseFragment() {
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    /*
     * 创建的时才加载
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {


        loadingDialog = new SweetAlertDialog(getContext());
        loadingDialog.setCancelable(true);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    loadingDialog.cancel();
                    if (onDialogBackPressed())
                        backHelper.backward();

                }
                return false;
            }
        });

        super.onCreate(savedInstanceState);
        /**
         *  SOFT_INPUT_ADJUST_NOTHING:         不调整(输入法完全直接覆盖住,未开放此参数)
         SOFT_INPUT_ADJUST_PAN:                 把整个Layout顶上去露出获得焦点的EditText,不压缩多余空间，见图1
         SOFT_INPUT_ADJUST_RESIZE:            整个Layout重新编排,重新分配多余空间，见图2
         SOFT_INPUT_ADJUST_UNSPECIFIED:  系统自己根据内容自行选择上两种方式的一种执行(默认配置)
         */

        context = getActivity();
        backHelper = new BackHelper(this);
        initData();
    }


    /**
     * 每次加载 都会调用onCreateView
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(getLayout(), container, false);
            view.setMinimumHeight(((WindowManager) getActivity()
                    .getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay().getHeight());
            view.setMinimumWidth(((WindowManager) getActivity()
                    .getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay().getWidth());

            unbinder = ButterKnife.bind(this, view);

        } else {
            ViewGroup viewParent = (ViewGroup) view.getParent();
            if (viewParent != null) {
                viewParent.removeAllViews();
                viewParent.addView(view);
                return viewParent;
            }
        }
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initListener();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            unbinder.unbind();
        } catch (Exception e) {

        }
        view = null;
    }

    /**
     * Fragment的布局
     *
     * @return
     */
    protected abstract int getLayout();

    protected abstract void initData();

    protected abstract void initView();

    protected abstract void initListener();

    /**
     * 是否相应dialog的返回按钮监听
     *
     * @return true：dialog显示状态按下返回键直接退出activity  false：dialog显示状态按下返回键cancel掉当前dialog
     */
    protected boolean onDialogBackPressed() {
        return true;
    }

    public void showLoading() {
        if (loadingDialog != null && !loadingDialog.isShowing())
            loadingDialog.show();
    }

    public void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
    }

    @Override
    public void onStop() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
        super.onStop();
    }

    public void showMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeToast(context, message).show();
    }

    public void showSuccessMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeSuccessToast(context, message).show();
    }

    public void showErrorMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeErrorToast(context, message).show();
    }

    public void showWarningMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeWarringToast(context, message).show();
    }
}
